package wechat.rcv;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import edu.fudan.mylang.pf.IObjectFactory;
import edu.fudan.mylang.utils.DateUtil;

@Controller
@Transactional
public class DetailsController {
	final static Logger logger = Logger.getLogger(DetailsController.class
			.getName());
	private IObjectFactory entities;

	@Autowired
	public DetailsController(IObjectFactory entities) {
		this.entities = entities;
	}
//	<a href="#"><span th:text="${clientname}"/></a>
//	<a href="#"><span th:text="${clientaddress}"/></a>
//	<a href="#"><span th:text="${projectname}"/></a>
//	<a href="#"><span th:text="${projectpriority}"/></a>
//	<a href="#"><span th:text="${reportdate}"/></a>
//	<a href="#"><span th:text="${reporttime}"/></a>
//	<a href="#"><span th:text="${plandate}"/></a>
//	<a href="#"><span th:text="${plantime}"/></a>
//	<a href="#"><span th:text="${tasktype}"/></a>
//	<a href="#"><span th:text="${taskstatus}"/></a>
//	<a href="#"><span th:text="${issuedesc}"/></a>
//	<a href="#"><span th:text="${assignee}"/></a>
//	<a href="#"><span th:text="${arrivaldate}"/></a>
//	<a href="#"><span th:text="${dispatchdate}"/></a>
//	<a href="#"><span th:text="${dispatchtime}"/></a>
//	<a href="#"><span th:text="${arrivalcheckin}"/></a>
	
	@RequestMapping("/taskdetails")
	public String Details(
			@RequestParam(value = "taskdesc", defaultValue = "") String taskdesc,
			Model model) {
		String page;
		logger.info("***************Details Request received!***************");
		try {
			setModelAttribute(model, "taskstatus","finished");
			setModelAttribute(model, "issuedesc", taskdesc);
			setModelAttribute(model, "arrivaldate",dateFormat(DateUtil.today())); //"上门日期"
			setModelAttribute(model, "arrivalcheckin","yes");
			page = "taskdetails_weui";
		} catch (Exception e) {
			e.printStackTrace();			
			setModelAttribute(model, "errormsg", e.getMessage().toString());
			page = "error";
		}
		return page;
	}

	private <T> T assertNotNull(String msg, T obj) {
		if (obj == null)
			throw new RuntimeException(msg + " is null");
		return obj;
	}
	private String timeFormat(Date day){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return day==null?null:format.format(day);
	}
	private String dateFormat(Date day){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return day==null?null:format.format(day);
	}
	private String hmFormat(Date day){
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		return day==null?null:format.format(day);
	}
	private String hmsFormat(Date day){
		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		return day==null?null:format.format(day);
	}
	private void setModelAttribute(Model model,  String key, String val){
		if ((val!=null)&&(!val.equals("")))
			model.addAttribute(key, val);
	}
	private String getLevelUIStr(String edlstring){
		HashMap<String, String> hmap = new HashMap<>();
		hmap.put("level1", "低");
		hmap.put("level2", "普通");
		hmap.put("level3", "高");
		hmap.put("level4", "紧急");
		return hmap.get(edlstring);
	}
	private String getStatusUIStr(String edlstring){
		HashMap<String, String> hmap = new HashMap<>();
		hmap.put("canceled", "已取消");
		hmap.put("closed", "已关闭");
		hmap.put("executing", "执行中");
		hmap.put("waitingForScheduling", "待调度");
		return hmap.get(edlstring);
	}
	private String dateTimeConcat(Date day, Date time){
		String dayStr = (day==null?null:dateFormat(day));
		String timeStr =(time==null?null:hmFormat(time));
		return dayStr==null?null:(dayStr+(timeStr==null?"":" "+timeStr));
	}
}